<?php

 mysql_connect('localhost','diandi_xiaoshuo','xiaoshuo!@#');

mysql_select_db("diandi_xiaoshuo");

mysql_query("set names utf8");
include_once dirname(__FILE__)."/../class/simple_html_dom.php";
// getPageData("/BookReader/2561219,44801478.aspx");exit;
getcaiji_sourceData();
getData();
function getData(){
	$sql = 'select * from caiji_pending where status=0 and site="qidian" order by id asc limit 5';
	$result = mysql_query($sql);
	while($row = mysql_fetch_array($result) ){
		echo $row['id']."\n";
		$url=$row['url'];
		getPageData($row);
		mysql_query("update caiji_pending set status=1 where id='".$row['id']."'");
	}
}

function getPageData($row){
	$url=$row['url'];
	$title=$row['title'];
	echo $url;
// $url=$row;
	$tmp = explode("/", $url);
	$u = $tmp[count($tmp)-1];
	$u= str_replace(",", "/", $u);
	$u= str_replace("aspx", "txt", $u);
	
// 	//$url1="http://www.92to.com/b/BaShu/".$url;
// 	/BookReader/2561219,44801478.aspx
// 	http://files.qidian.com/Author4/2561219/44801478.txt
	$url2="http://files.qidian.com/Author4/".$u;
	echo $url2;
// 	$html= file_get_html($url1);
// 	$title=strip_tags($html->find("h1",0)->innertext);
	$content= file_get_contents2($url2);
	if(empty($content)){echo 'error';exit;return;}
	
	
	$content=trim($content);
	$content=substr($content, 16,strlen($content)-19);
	$content = str_replace("http://www.qidian.com", "http://www.8shu.org", $content);
	$content = str_replace("www.qidian.com", "8shu(霸蜀)", $content);
	$content = str_replace("起点中文网", "", $content);
	$content = str_replace("最新、最快、最火的连载作品尽在起点原创！", "", $content);
	$content=trim($content);
	//$content="<p>".$content."</p>";
	//$p1=strpos($content, 'writeln');
	//$txt=substr($content, $p1+9,strlen($content)-9-3);
	//echo $title;exit;
	//$content = str_replace('()v', '', $content);
	//echo $content;exit;
	$sql="INSERT INTO `bashu_content` ( `asset_id`, `title`, `alias`, `title_alias`, `introtext`, `fulltext`, `state`, `sectionid`, `mask`, `catid`, `created`, `created_by`, `created_by_alias`, `modified`, `modified_by`, `checked_out`, `checked_out_time`, `publish_up`, `publish_down`, `images`, `urls`, `attribs`, `version`, `parentid`, `ordering`, `metakey`, `metadesc`, `access`, `hits`, `metadata`, `featured`, `language`, `xreference`) VALUES
(0, '".$title."', '".date("Y-m-d-m-h-s-").floor(microtime()*1000)."', '', '".mysql_escape_string($content)."', '".mysql_escape_string($content)."', 1, 0, 0, 9, '".date("Y-m-d m:h:s")."', 358, '', '0000-00-00 00:00:00', 0, 358, '".date("Y-m-d m:h:s")."', '".date("Y-m-d m:h:s")."', '0000-00-00 00:00:00', '{\"image_intro\":\"\",\"float_intro\":\"\",\"image_intro_alt\":\"\",\"image_intro_caption\":\"\",\"image_fulltext\":\"\",\"float_fulltext\":\"\",\"image_fulltext_alt\":\"\",\"image_fulltext_caption\":\"\"}',
 '{\"urla\":null,\"urlatext\":\"\",\"targeta\":\"\",\"urlb\":null,\"urlbtext\":\"\",\"targetb\":\"\",\"urlc\":null,\"urlctext\":\"\",\"targetc\":\"\"}', '{\"show_title\":\"\",\"link_titles\":\"\",\"show_intro\":\"\",\"show_category\":\"\",\"link_category\":\"\",\"show_parent_category\":\"\",\"link_parent_category\":\"\",\"show_author\":\"\",\"link_author\":\"\",\"show_create_date\":\"\",\"show_modify_date\":\"\",\"show_publish_date\":\"\",\"show_item_navigation\":\"\",\"show_icons\":\"\",\"show_print_icon\":\"\",\"show_email_icon\":\"\",\"show_vote\":\"\",\"show_hits\":\"\",\"show_noauth\":\"\",\"urls_position\":\"\",\"alternative_readmore\":\"\",\"article_layout\":\"\",\"show_publishing_options\":\"\",\"show_article_options\":\"\",\"show_urls_images_backend\":\"\",\"show_urls_images_frontend\":\"\"}'
 , 1, 0, 0, '', '', 1, 0, '{\"robots\":\"\",\"author\":\"\",\"rights\":\"\",\"xreference\":\"\"}', 0, '*', '');
	";
	//echo $sql;exit;
	if(!mysql_query($sql)){
		print_r(mysql_error());
	}
	$id=mysql_insert_id();
	mysql_query("update bashu_content set ordering=ordering+1");
	mysql_query("update bashu_content set `alias`='bashuchapter".$id."' where id='".$id."'");
	
}

function getcaiji_sourceData(){
	$url="http://read.qidian.com/BookReader/2561219.aspx";
	$html= file_get_html($url);
	//echo $html->find(".centent",0)->find("ul",1);
	foreach($html->find("#content",0)->find(".list",0)->find("a") as $p){
		$site='qidian';
		$url = $p->href;
		$data = array(
		'url'=>$url,
		'title'=>$p->innertext
		);
		//echo $url;exit;
		if(!checkFromDb($p->innertext,$site)){
			savecaiji_sourceToDb($data,$site);
		}
		//echo $p->innertext."<br>".$p->find('a',0)->href;
	}
	
}
function savecaiji_sourceToDb($data,$site){
	setTocaiji_pending($data['url'],$site,$data['title']);
	mysql_query("insert into caiji_source (url,code,status)values('".$data['url']."','".$data['title']."','1')");
}
function setTocaiji_pending($url,$site,$title){
	mysql_query("insert into caiji_pending (url,site,status,title)values('".$url."','".$site."','0','".$title."')");
}

function checkFromDb($data,$site){
	$sql="select count(*) num from caiji_source where code='".$data."'";
	$result=mysql_query($sql);
	$result= mysql_fetch_array($result);
	return isset($result[0]) && $result[0]>0;
}